package com.byq.mm.smartlib.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ToggleButton;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.byq.mm.application.cluster.provider.LocationProvider;
import com.byq.mm.smartlib.R;
import com.byq.mm.smartlib.cluster.server.AndroidLocationExecutor;

public class ServiceSettingsActivity extends AbstractSmartActivity {
	AndroidLocationExecutor exe;
	RadioGroup provider;
	RadioButton gps;
	RadioButton network;
	Button back;
	ToggleButton onoff;
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		this.setContentView(R.layout.service_settings);
		exe = (AndroidLocationExecutor) application.getRootLocalExecutors().get(LocationProvider.Define.CLUSTER_NAME);
		onoff =  (ToggleButton) this.findViewById(R.id.location_onoff);
		provider = (RadioGroup) this.findViewById(R.id.provider);
		gps = (RadioButton) this.findViewById(R.id.gps);
		network = (RadioButton) this.findViewById(R.id.network);
		back = (Button)this.findViewById(R.id.backSetting);

		String p = exe.getProvider();
		if(p.equals("network")){
			gps.setChecked(false);
			network.setChecked(true);
		}else{
			gps.setChecked(true);
			network.setChecked(false);
		}
		onoff.setChecked(exe.startted());
		
		provider.setOnCheckedChangeListener(new OnCheckedChangeListener(){
 
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(checkedId==gps.getId()){
					exe.setProvider(ServiceSettingsActivity.this,"gps");
				}
				if(checkedId==network.getId()){
					exe.setProvider(ServiceSettingsActivity.this, "network");
				}
			}
 
        }); 
		
		onoff.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				
				if(isChecked)
					exe.start();
				else
					exe.stop();
			}});
		back.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View view) {
				ServiceSettingsActivity.this.finish();
			}
		});

	}
	
	

}
